<?php

/**
 * This class handles the Router function that make sure we know what page to load
 * 
 * @author johan <info@johandahlberg.com>
 */
class router {
	/**
	 * @the registry
	 */
	private $registry;
	
	/**
	 * @the controller path
	 */
	private $path;
	
	/**
	 * @var the arguments passed through URL
	 */
	public $args = array();
	
	/**
	 * @var unknown_type
	 */
	public $file;
	
	/**
	 * @var unknown_type
	 */
	public $controller;
	
	/**
	 * @var unknown_type
	 */
	public $action;
	
	/**
	 * Create a new router and pass the registry
	 * 
	 * @param Registry $registry
	 */
	function __construct($registry) {
	        $this->registry = $registry;
	}
	
	/**
	 * @get the controller
	 *
	 * @access private
	 * @return void
	 */
	private function getController() {
        /*** get the route from the url ***/
        $route = (empty($_GET['rt'])) ? '' : $_GET['rt'];

        if (empty($route)) {
            $route = 'index';
        } else {
			/*** get the parts of the route ***/
            $parts = explode('/', $route);
            
            $this->controller = $parts[0];
            if(isset( $parts[1])) {
            	$this->action = $parts[1];
            }
            
            $index = 0;
            foreach($parts as $key => $value) {
            	if($index > 1) {
            		$this->args[] = $value;
            	}
            	
            	$index++;
            }
        }

        if (empty($this->controller)) {
            $this->controller = 'index';
        }

        /*** Get action ***/
        if (empty($this->action)) {
            $this->action = 'index';
        }

        /*** set the file path ***/
        $this->file = $this->path .'/'. ucfirst($this->controller) . 'Controller.php';
	}
	
	/**
	 * @load the controller
	 *
	 * @access public
	 * @return void
	 */
	public function loader() {
		/*** check the route ***/
        $this->getController();

        /*** if the file is not there diaf ***/
        if (is_readable($this->file) == false) {
        	$this->file = $this->path .'/'. 'ErrorController.php';
        	$this->controller = 'Error';
        }

        /*** include the controller ***/
        include $this->file;

        /*** a new controller class instance ***/
        $class = $this->controller . 'Controller';
        $controller = new $class($this->registry);

        /*** check if the action is callable ***/
        if (is_callable(array($controller, $this->action)) == false) {
            $action = 'index';
        } else {
			$action = $this->action;
        }
        
        /*** save router to Registry ***/
        $this->registry->router = $this;
        
        /*** run the action ***/
        $controller->$action();
	}
	
	/**
	 * @set controller directory path
	 *
	 * @param string $path
	 * @return void
	 *
	 */
	function setPath($path) {
        /*** check if path i sa directory ***/
        if (is_dir($path) == false) {
			throw new Exception ('Invalid controller path: `' . $path . '`');
        }
        /*** set the path ***/
        $this->path = $path;
	}
}
?>